package com.pay.controller;

import cn.hutool.extra.spring.SpringUtil;
import com.pay.common.core.api.base.BaseApiController;
import com.pay.common.core.api.base.BaseResponse;
import com.pay.entity.PaymentChannelDO;
import com.pay.entity.PaymentInfoDO;
import com.pay.service.open.api.OpenPaymentCoreService;
import com.pay.service.open.api.PaymentChannelService;
import com.pay.strategy.PayStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author 周益和
 * @ClassName PaymentController
 * @qq 1552951165
 */
@RestController
@RequestMapping("/pay")
public class PaymentController extends BaseApiController {
    @Autowired
    private PaymentChannelService paymentChannelService;
    @Autowired
    private OpenPaymentCoreService openPaymentCoreService;
    
    /**
     *
     * @param channelId
     * @param orderId
     * @return
     */
    @GetMapping("/payIdByResult/{channelId}")
    public BaseResponse<String> payIdByResult(@PathVariable String channelId,String orderId){
        // 1.验证参数
        if (StringUtils.isEmpty(channelId)) {
            return setResultError("channelId is null");
        }
        if (StringUtils.isEmpty(orderId)) {
            return setResultError("orderId is null");
        }
        // 2.寻找策略类
        PaymentChannelDO paymentChannelDO = paymentChannelService.getByChannelIdInfo(channelId);
        if (paymentChannelDO==null) {
            return setResultError("channelId is null");
        }
        // 3.查询支付信息
        PaymentInfoDO paymentInfoDO = openPaymentCoreService.getPaymentInfoByOrderId(orderId);
        if (paymentInfoDO == null) {
            return setResultError("订单号码错误");
        }
        String payBeanId = paymentChannelDO.getPayBeanId();
        PayStrategy payStrategy = SpringUtil.getBean(payBeanId, PayStrategy.class);
        boolean payIdByPayResult = payStrategy.payIdByPayResult(paymentInfoDO);
        //3.策略调用具体 主动根据订单号码同步状态
        return payIdByPayResult ? setResultSuccess():setResultError("同步状态失败");
    
    }
}
